package com.boyiis.sell.service.impl;

import com.boyiis.sell.enums.ResultEnum;
import com.boyiis.sell.exception.SellException;
import com.boyiis.sell.model.dto.CartDTO;
import com.boyiis.sell.model.entity.Product;
import com.boyiis.sell.enums.ProductStatusEnum;
import com.boyiis.sell.repository.ProductRepository;
import com.boyiis.sell.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductRepository productRepo;

    @Override
    public Product findOne(String productId) {
        return productRepo.findOne(productId);
    }

    @Override
    public Page<Product> findAll(Pageable pageable) {
        return productRepo.findAll(pageable);
    }

    @Override
    public Product save(Product product) {
        return productRepo.save(product);
    }

    @Override
    public List<Product> findUpAll() {
        return productRepo.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            Product product = productRepo.findOne(cartDTO.getProductId());
            if (null == product) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer num = product.getProductStock() + cartDTO.getProductQuantity();

            product.setProductStock(num);
            productRepo.save(product);
        }
    }

    @Override
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            Product product = productRepo.findOne(cartDTO.getProductId());
            if (null == product) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            Integer num = product.getProductStock() - cartDTO.getProductQuantity();
            if (num < 0) {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            product.setProductStock(num);

            productRepo.save(product);
        }
    }

    @Override
    public Product onSale(String productId) {
        Product product = productRepo.findOne(productId);
        if (null == product) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (product.getProductStatus() == ProductStatusEnum.UP.getCode()) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        product.setProductStatus(ProductStatusEnum.UP.getCode());
        product = productRepo.save(product);
        return product;
    }

    @Override
    public Product offSale(String productId) {
        Product product = productRepo.findOne(productId);
        if (null == product) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (product.getProductStatus() == ProductStatusEnum.DOWN.getCode()) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        product.setProductStatus(ProductStatusEnum.DOWN.getCode());
        product = productRepo.save(product);
        return product;
    }
}
